<?php
/**
 * WB_CacheClass class
 * 
 * @author Jean-Lou Dupont
 * @version @@package-version@@
 * @id $Id: CacheClass.php 93 2008-03-06 19:40:44Z jldupont $
 */

 //{{include
 require_once 'web-bloks/BaseClass/BaseClass.php';
 //include}}
 
abstract class WB_CacheClass
 	extends WB_BaseClass
 {
 	/**
 	 * A derived class must return
 	 * whether it is really a 'real' cache
 	 * or not.
 	 * 
 	 * @return boolean
 	 */
	public function isFake()
	{
		/* stub */	
		return true;
	} 	
	
	/**
	 * Returns information relative to the current cache.
	 * @return mixed
	 */
	public function getInfo()
	{ 
		/* stub */	
		return null;
	}

	/**
	 * This method must check the availability
	 * of the cache functionality in question.
	 */
	public static function checkPresence()
	{
		/* stub */
		return false;	
	}

	public function clearCache()
	{
		/* stub */
		return false;	
	}
	/* *** THE GUTS OF THE OPERATION *** */
	/* Override these with functional things in subclasses */

	function get($key) 
	{
		/* stub */
		return false;
	}

	function set($key, $value, $exptime=0) 
	{
		/* stub */
		return false;
	}

	function delete($key, $time=0) 
	{
		/* stub */
		return false;
	}

	function lock($key, $timeout = 0) 
	{
		/* stub */
		return true;
	}

	function unlock($key) 
	{
		/* stub */
		return true;
	}

	/* *** Emulated functions *** */
	/* Better performance can likely be got with custom written versions */
	function get_multi($keys) 
	{
		$out = array();
		foreach($keys as $key)
			$out[$key] = $this->get($key);
		return $out;
	}

	function set_multi($hash, $exptime=0) 
	{
		foreach($hash as $key => $value)
			$this->set($key, $value, $exptime);
	}

	function add($key, $value, $exptime=0) 
	{
		if( $this->get($key) === false ) 
		{
			$this->set($key, $value, $exptime);
			return true;
		}
		return false;
	}

	function add_multi($hash, $exptime=0) 
	{
		foreach($hash as $key => $value)
			$this->add($key, $value, $exptime);
	}

	function delete_multi($keys, $time=0) 
	{
		foreach($keys as $key)
			$this->delete($key, $time);
	}

	function replace($key, $value, $exptime=0) 
	{
		if( $this->get($key) !== false )
			$this->set($key, $value, $exptime);
	}
	/**
	 * Atomic Increment.
	 * @return false if lock can not be acquired.
	 */
	function incr($key, $value=1) 
	{
		if ( !$this->lock($key) )
			return false;

		$value = intval($value);
		if($value < 0) $value = 0;

		$n = false;
		if( ($n = $this->get($key)) !== false ) 
		{
			$n += $value;
			$this->set($key, $n); // exptime?
		}
		$this->unlock($key);
		return $n;
	}

	function decr($key, $value=1) 
	{
		if ( !$this->lock($key) ) 
			return false;

		$value = intval($value);
		if($value < 0) $value = 0;

		$m = false;
		if( ($n = $this->get($key)) !== false ) 
		{
			$m = $n - $value;
			if($m < 0) $m = 0;
			$this->set($key, $m); // exptime?
		}
		$this->unlock($key);
		return $m;
	}

	/**
	 * Convert an optionally relative time to an absolute time
	 */
	static function convertExpiry( $exptime ) 
	{
		if(($exptime != 0) && ($exptime < 3600*24*30)) 
		{
			return time() + $exptime;
		} 
		else 
		{
			return $exptime;
		}
	}
 } // end class declaration
